package common;

import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPBody;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl;
import com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl;

public class XmlParser {

	ArrayList<HashMap<String,String>> al = new ArrayList<HashMap<String,String>>();

	public XmlParser(){
	}

	public ArrayList<HashMap<String,String>> setXMLtoHashMap(Object message) {
		try {
			if(message != null){
			if (message.getClass() == Message1_1Impl.class) {
				SOAPBody soapbody = ((SOAPMessage) message).getSOAPBody();
				for (int i = 0; i < soapbody.getChildNodes().getLength(); i++) {
					if (soapbody.getNodeName().equals("Table")) {
						NodeList nodelist = ((Node) message).getChildNodes();
						HashMap<String,String> Hm = new HashMap<String,String>();
							for (int j = 0; j < nodelist.getLength(); j++) {
								Hm.put(nodelist.item(j).getNodeName(), nodelist.item(j).getTextContent());
							}
							al.add(Hm);
					}
				}
				if (soapbody.getNextSibling() == null) {
					setXMLtoHashMap(soapbody.getFirstChild());
				} else {
					setXMLtoHashMap(soapbody.getNextSibling());
				}
				
			} else if (message.getClass() == ElementImpl.class) {
				if (((Node) message).getNodeName().equals("Table")) {
					NodeList nodelist = ((Node) message).getChildNodes();
					HashMap<String,String> Hm = new HashMap<String,String>();
						for (int j = 0; j < nodelist.getLength(); j++) {
							Hm.put(nodelist.item(j).getNodeName(), nodelist.item(j).getTextContent());
						}
						al.add(Hm);
				}
				
				if (((Node) message).getNextSibling() == null) {
					setXMLtoHashMap(((Node) message).getFirstChild());
				} else {
					setXMLtoHashMap(((Node) message).getNextSibling());
				}
			}
			}
		} catch (Exception ee) {
			ee.printStackTrace();
		}

		return al;

	}
}
